package ui.activity.profit;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Html;
import android.text.Spanned;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jakewharton.rxbinding.view.RxView;
import com.jzxiang.pickerview.TimePickerDialog;
import com.jzxiang.pickerview.listener.OnDateSetListener;

import net.ck.hzq.R;

import java.util.Date;
import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

import Utils.SharedPrefStore;
import base.BaseAct;
import butterknife.BindView;
import coom.Constants;
import event.NetErrorEvent;
import ui.activity.profit.biz.ProfitDetailsBiz;
import ui.activity.profit.component.DaggerProfitDetailsComponent;
import ui.activity.profit.contract.ProfitDetailsContract;
import ui.activity.profit.module.ProfitDetailsModule;
import ui.activity.profit.presenter.ProfitDetailsPresenter;
import ui.model.ProfitDetailsBean;
import util.AlertUtils;
import util.StringUtils;
import util.SysUtils;

public class ProfitDetailsAct extends BaseAct implements ProfitDetailsContract.View {


    @Inject
    ProfitDetailsPresenter presenter;
    @Inject
    ProfitDetailsBiz biz;
    String Str = "";

    SharedPrefStore store;
    String customId;
    @BindView(R.id.profit_day)
    TextView day;
    @BindView(R.id.profit_check)
    TextView check;
    @BindView(R.id.profit_details_day)
    TextView detailsDay;
    @BindView(R.id.partners_image)
    ImageView partnersImage;
    @BindView(R.id.partners_expand_total)
    TextView partnersExpandTotal;
    @BindView(R.id.partners_expand_real)
    TextView partnersExpandReal;
    @BindView(R.id.partners_expand_unreal)
    TextView partnersExpandUnreal;
    @BindView(R.id.partners_expand_layout)
    LinearLayout partnersExpandLayout;
    @BindView(R.id.partners_promotion_total)
    TextView partnersPromotionTotal;
    @BindView(R.id.partners_promotion_real)
    TextView partnersPromotionReal;
    @BindView(R.id.partners_promotion_unreal)
    TextView partnersPromotionUnreal;
    @BindView(R.id.partners_promotion_layout)
    LinearLayout partnersPromotionLayout;
    @BindView(R.id.merchants_image)
    ImageView merchantsImage;
    @BindView(R.id.merchants_expand_total)
    TextView merchantsExpandTotal;
    @BindView(R.id.merchants_expand_real)
    TextView merchantsExpandReal;
    @BindView(R.id.merchants_expand_unreal)
    TextView merchantsExpandUnreal;
    @BindView(R.id.merchants_expand_layout)
    LinearLayout merchantsExpandLayout;
    @BindView(R.id.merchants_promotion_total)
    TextView merchantsPromotionTotal;
    @BindView(R.id.merchants_promotion_real)
    TextView merchantsPromotionReal;
    @BindView(R.id.merchants_promotion_unreal)
    TextView merchantsPromotionUnreal;
    @BindView(R.id.merchants_promotion_layout)
    LinearLayout merchantsPromotionLayout;
    @BindView(R.id.amount_image)
    ImageView amountImage;
    @BindView(R.id.amount_expand_total)
    TextView amountExpandTotal;
    @BindView(R.id.amount_expand_pos)
    TextView amountExpandPos;
    @BindView(R.id.amount_expand_epos)
    TextView amountExpandEpos;
    @BindView(R.id.amount_expand_qrcode)
    TextView amountExpandQrcode;
    @BindView(R.id.amount_expand_cloudPay)
    TextView amountExpandCloudPay;
    @BindView(R.id.amount_expand_layout)
    LinearLayout amountExpandLayout;
    @BindView(R.id.amount_promotion_total)
    TextView amountPromotionTotal;
    @BindView(R.id.amount_promotion_pos)
    TextView amountPromotionPos;
    @BindView(R.id.amount_promotion_epos)
    TextView amountPromotionEpos;
    @BindView(R.id.amount_promotion_qrcode)
    TextView amountPromotionQrcode;
    @BindView(R.id.amount_promotion_cloudPay)
    TextView amountPromotionCloudPay;
    @BindView(R.id.amount_promotion_layout)
    LinearLayout amountPromotionLayout;
    @BindView(R.id.income_image)
    ImageView incomeImage;
    @BindView(R.id.income_expand_total)
    TextView incomeExpandTotal;
    @BindView(R.id.income_expand_pos)
    TextView incomeExpandPos;
    @BindView(R.id.income_expand_epos)
    TextView incomeExpandEpos;
    @BindView(R.id.income_expand_qrcode)
    TextView incomeExpandQrcode;
    @BindView(R.id.income_expand_cloudPay)
    TextView incomeExpandCloudPay;
    @BindView(R.id.income_expand_layout)
    LinearLayout incomeExpandLayout;
    @BindView(R.id.income_promotion_total)
    TextView incomePromotionTotal;
    @BindView(R.id.income_promotion_pos)
    TextView incomePromotionPos;
    @BindView(R.id.income_promotion_epos)
    TextView incomePromotionEpos;
    @BindView(R.id.income_promotion_qrcode)
    TextView incomePromotionQrcode;
    @BindView(R.id.income_promotion_cloudPay)
    TextView incomePromotionCloudPay;
    @BindView(R.id.income_promotion_layout)
    LinearLayout incomePromotionLayout;
    @BindView(R.id.income_ji_huo)
    TextView incomeJiHuo;
    @BindView(R.id.income_ji_money)
    TextView incomeJiMoney;
    @BindView(R.id.income_ji_layout)
    LinearLayout incomeJiLayout;
    @BindView(R.id.profit_layout1)
    TextView profitLayout1;
    @BindView(R.id.profit_layout2)
    TextView profitLayout2;
    @BindView(R.id.profit_layout3)
    TextView profitLayout3;
    @BindView(R.id.profit_layout4)
    TextView profitLayout4;

    long selectTime;
    private int[][] textView1 = {{R.id.profit_title1, R.id.profit_title2}, {R.id.profit_title3, R.id.profit_title4},
            {R.id.profit_title5, R.id.profit_title6}, {R.id.profit_title7, R.id.profit_title8, R.id.profit_title9}};

    @Override
    protected void setRoot() {
//        setTitleBar(R.color.colorBlue);
        setContentView(R.layout.activity_profit_details);
    }

    @Override
    protected void setup() {
        DaggerProfitDetailsComponent.builder().profitDetailsModule(new ProfitDetailsModule(this)).build().inject(this);
        setToolBar(getResources().getString(R.string.lang_money_detail), "");
        presenter.setBiz(biz);
    }


    @Override
    protected void init(@Nullable Bundle savedInstanceState) {
        selectTime = System.currentTimeMillis();
        Str = StringUtils.isEmpty(getIntent().getStringExtra(Constants.KEYSTRING)) ? SysUtils.sf1.format(new Date(System.currentTimeMillis())) :
                SysUtils.sf2.format(new Date(System.currentTimeMillis())).equals(getIntent().getStringExtra(Constants.KEYSTRING)) ?
                        SysUtils.sf1.format(new Date(System.currentTimeMillis())) : getIntent().getStringExtra(Constants.KEYSTRING) + "-01";
        store = new SharedPrefStore();
        customId = store.getString("id", "0");
        detailsDay.setText(Str);
        day.setText(SysUtils.sf1.format(new Date(System.currentTimeMillis())));
        RxView.touches(day)
                .compose(this.<MotionEvent>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(MotionEvent -> {
                    TimePickerDialog timePickerDialog = AlertUtils.timePicker4(ProfitDetailsAct.this, 365 * 5, 0, selectTime, new OnDateSetListener() {
                        @Override
                        public void onDateSet(TimePickerDialog timePickerView, long millseconds) {
                            selectTime = millseconds;
                            day.setText(SysUtils.sf1.format(new Date(millseconds)));
                            detailsDay.setText(SysUtils.sf1.format(new Date(millseconds)));
                            Str = SysUtils.sf1.format(new Date(millseconds));
                        }
                    });
                    timePickerDialog.show(ProfitDetailsAct.this.getSupportFragmentManager(), "all");
                });
        RxView.clicks(check)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(Void -> presenter.getInfo(customId, Str));
        RxView.clicks(partnersImage)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(Void -> presenter.toUpdateVisibility(0));
        RxView.clicks(merchantsImage)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(Void -> presenter.toUpdateVisibility(1));
        RxView.clicks(amountImage)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(Void -> presenter.toUpdateVisibility(2));
        RxView.clicks(incomeImage)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(Void -> presenter.toUpdateVisibility(3));
        RxView.clicks(profitLayout1)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(Void -> presenter.toUpdateVisibility(0));
        RxView.clicks(profitLayout2)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(Void -> presenter.toUpdateVisibility(1));
        RxView.clicks(profitLayout3)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(Void -> presenter.toUpdateVisibility(2));
        RxView.clicks(profitLayout4)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(Void -> presenter.toUpdateVisibility(3));
        presenter.getInfo(customId, Str);
    }


    @Override
    public void onNetErrorEvent(NetErrorEvent event) {
        super.onNetErrorEvent(event);
    }

    @Override
    protected void onNetError(NetErrorEvent event) {

    }


    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        biz.onDestory();
    }


    @Override
    public void upDateUI(ProfitDetailsBean bean) {

        if (bean.getDetailList() != null && bean.getDetailList().size() > 0) {
            if (bean.getDetailList().get(0).getNewPartners() != null) {
                if (bean.getDetailList().get(0).getNewPartners().getExpandPartners() != null) {
                    ProfitDetailsBean.DetailListBean.NewPartnersBean.ExpandPartnersBean expandPartnersBean = bean.getDetailList().get(0).getNewPartners().getExpandPartners();
                    partnersExpandTotal.setText(expandPartnersBean.getTotal());
                    Spanned strA = Html.fromHtml("已实名：<font color=#0CA6F0>" + expandPartnersBean.getRealName() + "</font>");
                    partnersExpandReal.setText(expandPartnersBean.getRealName());
                    partnersExpandUnreal.setText(expandPartnersBean.getUnRealName());
                }

                if (bean.getDetailList().get(0).getNewPartners().getPromotionPartners() != null) {
                    ProfitDetailsBean.DetailListBean.NewPartnersBean.PromotionPartnersBean promotionPartnersBean = bean.getDetailList().get(0).getNewPartners().getPromotionPartners();
                    partnersPromotionTotal.setText(promotionPartnersBean.getTotal());
                    Spanned strA = Html.fromHtml("已实名：<font color=#0CA6F0>" + promotionPartnersBean.getRealName() + "</font>");
                    partnersPromotionReal.setText( promotionPartnersBean.getRealName());
                    partnersPromotionUnreal.setText( promotionPartnersBean.getUnRealName());
                }
            }
            if (bean.getDetailList().get(0).getNewMerchants() != null) {
                if (bean.getDetailList().get(0).getNewMerchants().getExpandMerchants() != null) {
                    ProfitDetailsBean.DetailListBean.NewMerchantsBean.ExpandMerchantsBean expandMerchantsBean = bean.getDetailList().get(0).getNewMerchants().getExpandMerchants();
                    merchantsExpandTotal.setText(expandMerchantsBean.getTotal());
                    Spanned strA = Html.fromHtml("已激活：<font color=#0CA6F0>" + expandMerchantsBean.getRealName() + "</font>");
                    merchantsExpandReal.setText(expandMerchantsBean.getRealName());
                    merchantsExpandUnreal.setText(expandMerchantsBean.getUnRealName());
                }

                if (bean.getDetailList().get(0).getNewMerchants().getPromotionMerchants() != null) {
                    ProfitDetailsBean.DetailListBean.NewMerchantsBean.PromotionMerchantsBean promotionMerchantsBean = bean.getDetailList().get(0).getNewMerchants().getPromotionMerchants();
                    merchantsPromotionTotal.setText(promotionMerchantsBean.getTotal());
                    Spanned strA = Html.fromHtml("已激活：<font color=#0CA6F0>" + promotionMerchantsBean.getRealName() + "</font>");
                    merchantsPromotionReal.setText(promotionMerchantsBean.getRealName());
                    merchantsPromotionUnreal.setText( promotionMerchantsBean.getUnRealName());
                }
            }

            if (bean.getDetailList().get(0).getTotalAmount() != null) {
                if (bean.getDetailList().get(0).getTotalAmount().getExpandAmount() != null) {
                    ProfitDetailsBean.DetailListBean.TotalAmountBean.ExpandAmountBean expandAmount = bean.getDetailList().get(0).getTotalAmount().getExpandAmount();
                    amountExpandTotal.setText(expandAmount.getTotal());
                    amountExpandPos.setText(expandAmount.getPos());
                    amountExpandEpos.setText(expandAmount.getEpos());
                    amountExpandQrcode.setText(expandAmount.getQrcode());
                    amountExpandCloudPay.setText(expandAmount.getCloudPay());
                }
                if (bean.getDetailList().get(0).getTotalAmount().getPromotionAmount() != null) {
                    ProfitDetailsBean.DetailListBean.TotalAmountBean.PromotionAmountBean promotionAmountBean = bean.getDetailList().get(0).getTotalAmount().getPromotionAmount();
                    amountPromotionTotal.setText(promotionAmountBean.getTotal());
                    amountPromotionPos.setText(promotionAmountBean.getPos());
                    amountPromotionEpos.setText(promotionAmountBean.getEpos());
                    amountPromotionQrcode.setText(promotionAmountBean.getQrcode());
                    amountPromotionCloudPay.setText(promotionAmountBean.getCloudPay());
                }
            }

            if (bean.getDetailList().get(0).getIncome() != null) {
                if (bean.getDetailList().get(0).getIncome().getExpandIncome() != null) {
                    ProfitDetailsBean.DetailListBean.IncomeBean.ExpandIncomeBean expandIncomeBean = bean.getDetailList().get(0).getIncome().getExpandIncome();
                    incomeExpandTotal.setText(expandIncomeBean.getTotal());
                    incomeExpandPos.setText(expandIncomeBean.getPos());
                    incomeExpandEpos.setText(expandIncomeBean.getEpos());
                    incomeExpandQrcode.setText(expandIncomeBean.getQrcode());
                    incomeExpandCloudPay.setText(expandIncomeBean.getCloudPay());
                }
                if (bean.getDetailList().get(0).getIncome().getPromotionIncome() != null) {
                    ProfitDetailsBean.DetailListBean.IncomeBean.PromotionIncomeBean promotionIncomeBean = bean.getDetailList().get(0).getIncome().getPromotionIncome();
                    incomePromotionTotal.setText(promotionIncomeBean.getTotal());
                    incomePromotionPos.setText(promotionIncomeBean.getPos());
                    incomePromotionEpos.setText(promotionIncomeBean.getEpos());
                    incomePromotionQrcode.setText(promotionIncomeBean.getQrcode());
                    incomePromotionCloudPay.setText(promotionIncomeBean.getCloudPay());
                }
                if (bean.getDetailList().get(0).getIncome().getActIncome() != null) {
                    ProfitDetailsBean.DetailListBean.IncomeBean.ActIncomeBean actIncomeBean = bean.getDetailList().get(0).getIncome().getActIncome();
                    incomeJiHuo.setText(actIncomeBean.getActIncome());
                    incomeJiMoney.setText(actIncomeBean.getActIncome());
                }

            }

        }

    }

    @Override
    public void upDateVisibility(int type) {
        switch (type) {
            case 0:
                if (partnersExpandLayout.getVisibility() == View.VISIBLE) {
                    partnersExpandLayout.setVisibility(View.GONE);
                    partnersPromotionLayout.setVisibility(View.GONE);
                    partnersImage.setBackgroundResource(R.mipmap.arrow_down_xi);
                    setColor(type, Color.parseColor("#666666"));
                } else {
                    partnersExpandLayout.setVisibility(View.VISIBLE);
                    partnersPromotionLayout.setVisibility(View.VISIBLE);
                    partnersImage.setBackgroundResource(R.mipmap.arrow_up_xi);
                    setColor(type, Color.parseColor("#0CA6F0"));
                }
                break;
            case 1:
                if (merchantsExpandLayout.getVisibility() == View.VISIBLE) {
                    merchantsExpandLayout.setVisibility(View.GONE);
                    merchantsPromotionLayout.setVisibility(View.GONE);
                    merchantsImage.setBackgroundResource(R.mipmap.arrow_down_xi);
                    setColor(type, Color.parseColor("#666666"));
                } else {
                    merchantsExpandLayout.setVisibility(View.VISIBLE);
                    merchantsPromotionLayout.setVisibility(View.VISIBLE);
                    merchantsImage.setBackgroundResource(R.mipmap.arrow_up_xi);
                    setColor(type, Color.parseColor("#0CA6F0"));
                }
                break;
            case 2:
                if (amountExpandLayout.getVisibility() == View.VISIBLE) {
                    amountExpandLayout.setVisibility(View.GONE);
                    amountPromotionLayout.setVisibility(View.GONE);
                    amountImage.setBackgroundResource(R.mipmap.arrow_down_xi);
                    setColor(type, Color.parseColor("#666666"));
                } else {
                    amountExpandLayout.setVisibility(View.VISIBLE);
                    amountPromotionLayout.setVisibility(View.VISIBLE);
                    amountImage.setBackgroundResource(R.mipmap.arrow_up_xi);
                    setColor(type, Color.parseColor("#F52626"));
                }
                break;
            case 3:
                if (incomeExpandLayout.getVisibility() == View.VISIBLE) {
                    incomeExpandLayout.setVisibility(View.GONE);
                    incomePromotionLayout.setVisibility(View.GONE);
                    incomeImage.setBackgroundResource(R.mipmap.arrow_down_xi);
                    incomeJiLayout.setVisibility(View.GONE);
                    setColor(type, Color.parseColor("#666666"));
                } else {
                    incomeExpandLayout.setVisibility(View.VISIBLE);
                    incomePromotionLayout.setVisibility(View.VISIBLE);
                    incomeImage.setBackgroundResource(R.mipmap.arrow_up_xi);
                    incomeJiLayout.setVisibility(View.VISIBLE);
                    setColor(type, Color.parseColor("#F52626"));
                }
                break;
            default:
                break;
        }
    }

    /**
     * 设置文字颜色
     *
     * @param type  类型
     * @param color 颜色
     */
    private void setColor(int type, int color) {
        for (int index : textView1[type]) {
            ((TextView) findViewById(index)).setTextColor(color);
        }
    }
}

